package com.mobile.web.config;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mobile.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.mobile.service.inter.system.FunctionService;
import com.mobile.service.inter.system.UserService;
import com.mobile.web.base.BaseController;

/**
 * 静态等非权限请求API接口
 *
 * @author Administrator
 * @version 0.1 2021-01-15 初版
 *
 */
@RestController
@RequestMapping("/noper")
public class NoPermission  extends BaseController{

	@Resource(name="userService")
	private UserService userService;

	@Autowired
	FunctionService functionService;



	/**
	 * 没有登录返回401状态码，前端跳转登录页
	 * @return
	 */
	@RequestMapping(value = "/toLogin", method = RequestMethod.GET)
	public ResponseEntity<Map<String, Object>> toLogin() {
		Map<String, Object> map = new HashMap<>();
		map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
		map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("W0012"));
		return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(map);
	}

	/**
	 * 没有权限返回403状态码，前端提示没有权限信息
	 * @return
	 */
	@RequestMapping(value = "/noAuth")
	public ResponseEntity<Map<String, Object>> noAuth() {
		Map<String, Object> map = new HashMap<>();
		map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
		map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("W0013"));
		return ResponseEntity.status(HttpStatus.FORBIDDEN).body(map);
	}

	/**
	 * 账户被禁用
	 * @return
	 */
	@RequestMapping("/locked")
	public ResponseEntity<Map<String, Object>> locked() {
		Map<String, Object> map = new HashMap<>();
		map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
		map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0051"));
		return ResponseEntity.status(HttpStatus.OK).body(map);
	}

	/**
	 * 图片验证码
	 */
	@RequestMapping(value = "/getKeyword",method = RequestMethod.POST)
	@ResponseBody
	public Object getKeyword(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> map = new HashMap<>();
		try {
			map = VCodeUtils.imageToBase64(120, 40, VCodeUtils.getStringRandom(4));
			map.put(Const.ACTION_RESULT,Const.ACTION_RESULT_SUCCESS);
		} catch (Exception e) {
			map.put(Const.ACTION_RESULT,Const.ACTION_RESULT_EXCEPTION);
			map.put(Const.ACTION_MESSAGE,e.toString());
			e.printStackTrace();
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 获取手机验证码
	 */
//	@RequestMapping(value = "/getUpdatePasswordKeyword",method = RequestMethod.POST)
//	@ResponseBody
//	public Object getUpdatePasswordKeyword(){
//		PageData pd = this.getPageData();
//		Map<String, Object> map = new HashMap<>();
//		try {
//			PageData pageData = userService.getUserByPhone(pd.getString("phone"));
//			if(StringUtil.isEmpty(pageData) && "1".equals(pd.getString("type"))){
//				map.put(Const.ACTION_RESULT,Const.ACTION_RESULT_FAILED);
//				map.put(Const.ACTION_MESSAGE,MessageUtil.getMessage("E0021"));
//				return AppUtil.returnObject(new PageData(), map);
//			}
////			Map<String,Object> status = sendsms.sendsCheckNum(pd.getString("phone"));
//			map.put(Const.ACTION_RESULT,Const.ACTION_RESULT_SUCCESS);
//			map.put("keyword",status.get("checkNum"));
//		} catch (Exception e) {
//			map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
//			map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
//			logger.error(e.getMessage());
//		}
//		return map;
//	}

	/**
	 * 修改密码
	 */
	@RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
	@ResponseBody
	public Object updatePassword(){
		PageData pd = this.getPageData();
		Map<String, Object> map = new HashMap<>();
		try {
			PageData pageData = userService.getUserByPhone(pd.getString("phone"));
			pd.put("secret_key",pageData.getString("secret_key"));
			//判断新旧密码是否一致,一致不允许修改
			if (EncryptUtil.encrypt(pd.getString("passwd").trim() + pd.getString("secret_key")).equals(pageData.getString("user_password"))) {
				map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
				map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0015"));
				return AppUtil.returnObject(new PageData(), map);
			}
			int status = userService.updatePassword(pd);
			map.put(Const.ACTION_RESULT,Const.ACTION_RESULT_SUCCESS);
			map.put("data",status);
		} catch (Exception e) {
			map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
			map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
			logger.error(e.getMessage(), e);
		}
		return AppUtil.returnObject(map);
	}




}
